package com.sangfor.ssl;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.os.Process;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.IConstants;
import com.sangfor.ssl.common.EnumHelperUtil;
import com.sangfor.ssl.common.ErrorCode;
import com.sangfor.ssl.common.ReasonCode;
import com.sangfor.ssl.common.SFError;
import com.sangfor.ssl.common.Values;
import com.sangfor.ssl.common.VpnCommon;
import com.sangfor.ssl.easyapp.SangforNbAuth;
import com.sangfor.ssl.l3vpn.service.AppStore;
import com.sangfor.ssl.l3vpn.service.DTSangforTunnelSocket;
import com.sangfor.ssl.l3vpn.service.VpnServiceManager;
import com.sangfor.ssl.l3vpn.service.VpnStatus;
import com.sangfor.ssl.service.auth.AuthNativesManager;
import com.sangfor.ssl.service.line.LineResult;
import com.sangfor.ssl.service.line.LineSelectionTask;
import com.sangfor.ssl.service.line.SelectLineCallback;
import com.sangfor.ssl.service.netmonitor.NetworkHelper;
import com.sangfor.ssl.service.netmonitor.NetworkMonitor;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.setting.SystemConfiguration;
import com.sangfor.ssl.service.utils.IGeneral;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/* loaded from: classes2.dex */
public class SangforAuthManager implements SelectLineCallback, IVpnDelegate {
    private static final int DEFAULT_PORT = 443;
    private static final int DETECT_TUNNEL_DEFAULT_DELAY_MILLIS = 100;
    private static final int DETECT_TUNNEL_MAX_COUNT = 100;
    private static final String EXCEPTION_LOGIN_RESULT_LISTENER = "LoginResultListener should not be null.";
    private static final String EXCEPTION_RAND_CODE_LISTENER = "RandCodeListener should not be null.";
    private static final String EXCEPTION_STATUS_CHANGE_LISTENER = "StatusChangeListener should not be null.";
    private static final String TAG = "SangforAuthManager";
    private SangforAuth mSFAuth;
    private static final SangforAuthManager INSTANCE = new SangforAuthManager();
    public static final ExecutorService VPN_AUTH_TASK_EXECUTOR = Executors.newSingleThreadExecutor();
    private static int mCurrentDetectTunnelCount = 0;
    public static String SDK_JAVA_VERSION = "M7.6.3_01_JAVA_23";
    private static IConstants.VPNStatus sVpnStatus = IConstants.VPNStatus.VPNOFFLINE;
    Handler mainHandler = new Handler(Looper.getMainLooper());
    private WeakReference<LoginResultListener> mLoginResultListener = null;
    private WeakReference<RandCodeListener> mRandCodeListener = null;
    private Application mApplication = null;
    private boolean mIsCallBacked = false;
    private int mLogLevel = 4;
    private IConstants.SdkMode mSdkMode = IConstants.SdkMode.COMMON;
    private String mAddress = "";
    private String mHostStr = "";
    private int mLoginType = 17;
    private int mVPNMode = 2;
    private int mAuthConnectTimeOut = 30;
    private String mUserPassword = null;
    private String mDefaultLanguage = IGeneral.CN_LANGUAGE;
    private boolean mAutoLoginOff = false;

    /* loaded from: classes2.dex */
    interface DNSCallback {
        void doAfterDNSTask(boolean z, String str);
    }

    static /* synthetic */ int access$004() {
        int i = mCurrentDetectTunnelCount + 1;
        mCurrentDetectTunnelCount = i;
        return i;
    }

    public static SangforAuthManager getInstance() {
        return INSTANCE;
    }

    private SangforAuthManager() {
        this.mSFAuth = null;
        this.mSFAuth = SangforAuth.getInstance();
    }

    public Application getmApplication() {
        return this.mApplication;
    }

    public SangforAuthManager setLoginResultListener(LoginResultListener loginResultListener) throws SFException {
        if (loginResultListener == null) {
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mLoginResultListener = new WeakReference<>(loginResultListener);
        return this;
    }

    public SangforAuthManager setRandCodeListener(RandCodeListener randCodeListener) throws SFException {
        if (randCodeListener == null) {
            throw new SFException(EXCEPTION_RAND_CODE_LISTENER);
        }
        this.mRandCodeListener = new WeakReference<>(randCodeListener);
        return this;
    }

    public SangforAuthManager setDeviceLanguage(IConstants.DeviceLanguage deviceLanguage) {
        if (deviceLanguage == null) {
            Log.warn(TAG, "Language param is null.");
            this.mDefaultLanguage = IGeneral.CN_LANGUAGE;
        } else if (AnonymousClass8.$SwitchMap$com$sangfor$ssl$IConstants$DeviceLanguage[deviceLanguage.ordinal()] == 1) {
            this.mDefaultLanguage = IGeneral.EN_LANGUAGE;
        } else {
            this.mDefaultLanguage = IGeneral.CN_LANGUAGE;
        }
        Values.initStringsLanguage(this.mDefaultLanguage);
        return this;
    }

    public SangforAuthManager setAuthConnectTimeOut(int i) {
        if (i <= 0) {
            this.mAuthConnectTimeOut = 30;
        } else {
            this.mAuthConnectTimeOut = i;
        }
        return this;
    }

    public SangforAuthManager setLogLevel(IConstants.LogLevel logLevel) {
        if (logLevel == null) {
            this.mLogLevel = 4;
        } else {
            int i = AnonymousClass8.$SwitchMap$com$sangfor$ssl$IConstants$LogLevel[logLevel.ordinal()];
            if (i == 1) {
                this.mLogLevel = 5;
            } else if (i == 2) {
                this.mLogLevel = 6;
            } else if (i == 3) {
                this.mLogLevel = 7;
            } else {
                this.mLogLevel = 4;
            }
        }
        return this;
    }

    public SangforAuthManager addAllowedL3VPNApps(List<String> list) {
        AppStore.getInstance().addWhiteAppList(list);
        return this;
    }

    public SangforAuthManager disableAutoLogin() {
        this.mAutoLoginOff = true;
        return this;
    }

    /* JADX INFO: Access modifiers changed from: protected */
    public int getLogLevel() {
        return this.mLogLevel;
    }

    public String getSession() {
        String twfid;
        return (this.mSdkMode == IConstants.SdkMode.BYPASS || (twfid = this.mSFAuth.getTwfid()) == null) ? "" : twfid;
    }

    public void changeVpnMode(int i) {
        this.mVPNMode = i;
    }

    public int getVpnMode() {
        return this.mVPNMode;
    }

    public void onActivityResult(int i, int i2) {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        this.mSFAuth.onActivityResult(i, i2);
    }

    public SmsMessage reacquireSmsCode() {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return new SmsMessage();
        }
        if (this.mSFAuth.vpnRegetSmsCodeBlock() != 0) {
            return null;
        }
        return (SmsMessage) getAuthMessage(2);
    }

    public void reacquireRandCode() {
        if (this.mSdkMode != IConstants.SdkMode.BYPASS && SangforAuth.getInstance().vpnGetRndCode() == -1) {
            Log.warn(TAG, "reacquire rand code fail.");
        }
    }

    public SangforAuthManager addStatusChangedListener(OnStatusChangedListener onStatusChangedListener) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return this;
        }
        if (onStatusChangedListener == null) {
            throw new SFException(EXCEPTION_STATUS_CHANGE_LISTENER);
        }
        StatusChangeManager.getInstance().addStatusChangedListener(onStatusChangedListener);
        return this;
    }

    public SangforAuthManager removeStatusChangedListener(OnStatusChangedListener onStatusChangedListener) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return this;
        }
        if (onStatusChangedListener == null) {
            throw new SFException(EXCEPTION_STATUS_CHANGE_LISTENER);
        }
        StatusChangeManager.getInstance().removeStatusChangedListener(onStatusChangedListener);
        return this;
    }

    public SangforAuthManager clearStatusChangedListener() {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return this;
        }
        StatusChangeManager.getInstance().clearStatusChangedListener();
        return this;
    }

    public IConstants.VPNStatus queryStatus() {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return IConstants.VPNStatus.VPNOFFLINE;
        }
        if (this.mSFAuth.queryVpnStatus() == 12) {
            return IConstants.VPNStatus.VPNONLINE;
        }
        return IConstants.VPNStatus.VPNOFFLINE;
    }

    public ChangePasswordResult changePassword(String str, String str2) {
        ChangePasswordResult changePasswordResult = new ChangePasswordResult();
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return changePasswordResult;
        }
        changePasswordResult.isSuccess = false;
        if (queryStatus() == IConstants.VPNStatus.VPNOFFLINE) {
            changePasswordResult.resultStr = Values.strings.CHANGE_PASSWORD_ILLEGAL;
            return changePasswordResult;
        } else if (str == null || str2 == null) {
            changePasswordResult.resultStr = Values.strings.CHANGE_PASSWORD_VALUE_EMPTY;
            return changePasswordResult;
        } else if (TextUtils.equals(str, str2)) {
            changePasswordResult.resultStr = Values.strings.CHANGE_PASSWORD_NEW_PASSWORD_SAME_AS_OLD;
            return changePasswordResult;
        } else {
            return this.mSFAuth.changePassword(str, str2);
        }
    }

    public void startPasswordAuthLogin(Application application, Activity activity, IConstants.VPNMode vPNMode, URL url, String str, String str2) throws SFException {
        Log.info(TAG, "startPasswordAuthLogin invoked", new Throwable());
        this.mIsCallBacked = false;
        initSdkMode(application);
        clearTicketInfo();
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        } else if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            sendSuccessMessage();
        } else if (!checkVPNInitParameters(application, activity, vPNMode) || url == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
        } else if (TextUtils.isEmpty(str) || str2 == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_PASSWOR_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_PASSWOR_INVALID));
        } else if (!NetworkHelper.isNetworkConnected(application.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            try {
                vpnModeInit(application, activity, vPNMode);
                initCommonParams();
                this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_USERNAME, str);
                this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_PASSWORD, str2);
                this.mLoginType = 1;
                this.mApplication = application;
                this.mUserPassword = str2;
                startVpnLine(dealVPNAddress(url));
            } catch (SFException e) {
                Log.error(TAG, Log.getExceptionContent(e));
                sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
            }
        }
    }

    public void startCertificateAuthLogin(Application application, Activity activity, IConstants.VPNMode vPNMode, URL url, String str, String str2) throws SFException {
        Log.info(TAG, "startCertificateAuthLogin invoked", new Throwable());
        this.mIsCallBacked = false;
        initSdkMode(application);
        clearTicketInfo();
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        } else if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            sendSuccessMessage();
        } else if (!checkVPNInitParameters(application, activity, vPNMode) || url == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
        } else if (TextUtils.isEmpty(str) || str2 == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(application.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            try {
                vpnModeInit(application, activity, vPNMode);
                initCommonParams();
                this.mSFAuth.setLoginParam(IVpnDelegate.CERT_P12_FILE_NAME, str);
                this.mSFAuth.setLoginParam(IVpnDelegate.CERT_PASSWORD, str2);
                this.mLoginType = 0;
                this.mApplication = application;
                startVpnLine(dealVPNAddress(url));
            } catch (SFException e) {
                Log.error(TAG, Log.getExceptionContent(e));
                sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
            }
        }
    }

    public void startSessionAuthLogin(Application application, Activity activity, IConstants.VPNMode vPNMode, URL url, String str) throws SFException {
        this.mIsCallBacked = false;
        initSdkMode(application);
        clearTicketInfo();
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        } else if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            sendSuccessMessage();
        } else if (!checkVPNInitParameters(application, activity, vPNMode) || url == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
        } else if (TextUtils.isEmpty(str)) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(application.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            try {
                vpnModeInit(application, activity, vPNMode);
                initCommonParams();
                this.mSFAuth.setLoginParam(IVpnDelegate.TWF_AUTH_TWFID, str);
                this.mLoginType = 16;
                this.mApplication = application;
                startVpnLine(dealVPNAddress(url));
            } catch (SFException e) {
                Log.error(TAG, Log.getExceptionContent(e));
                sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
            }
        }
    }

    public void startDingTalkCodeAuthLogin(Application application, Activity activity, IConstants.VPNMode vPNMode, URL url, String str, String str2) throws SFException {
        this.mIsCallBacked = false;
        initSdkMode(application);
        clearTicketInfo();
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        } else if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            sendSuccessMessage();
        } else if (!checkVPNInitParameters(application, activity, vPNMode) || url == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
        } else if (TextUtils.isEmpty(str2)) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(application.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            try {
                vpnModeInit(application, activity, vPNMode);
                initCommonParams();
                this.mSFAuth.setLoginParam(IVpnDelegate.CODE_AUTH_TYPE, str);
                this.mSFAuth.setLoginParam(IVpnDelegate.CODE_AUTH_INFO, str2);
                this.mLoginType = 11;
                this.mApplication = application;
                startVpnLine(dealVPNAddress(url));
            } catch (SFException e) {
                Log.error(TAG, Log.getExceptionContent(e));
                sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
            }
        }
    }

    public boolean setDnsWhiteList(List<String> list) {
        return this.mSFAuth.setDnsWhiteList(list);
    }

    public void startTicketAuthLogin(Application application, Activity activity, IConstants.VPNMode vPNMode) throws SFException {
        this.mIsCallBacked = false;
        initSdkMode(application);
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        } else if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            sendSuccessMessage();
        } else {
            TicketAuthManager.getInstance().init(application);
            if (!checkVPNInitParameters(application, activity, vPNMode) || !ticketAuthAvailable(application)) {
                Log.info(TAG, "ticket auth invalid");
                sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
            } else if (!NetworkHelper.isNetworkConnected(application.getApplicationContext())) {
                Log.info(TAG, "Network is not enable.");
                sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
            } else {
                try {
                    vpnModeInit(application, activity, vPNMode);
                    String str = TicketAuthManager.getInstance().getmTicketCode();
                    String str2 = TicketAuthManager.getInstance().getmVpnAddress();
                    initCommonParams();
                    this.mSFAuth.setLoginParam("auth.ticket.key", str);
                    this.mLoginType = 2048;
                    this.mApplication = application;
                    if (!str2.startsWith(IGeneral.PROTO_HTTPS_HEAD) && !str2.startsWith(IGeneral.PROTO_HTTP_HEAD)) {
                        str2 = String.format(Locale.getDefault(), "https://%s", str2);
                    }
                    try {
                        startVpnLine(dealVPNAddress(new URL(str2)));
                    } catch (MalformedURLException unused) {
                        Log.error(TAG, "vpn address invalid");
                        sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
                    }
                } catch (SFException e) {
                    Log.error(TAG, Log.getExceptionContent(e));
                    sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
                }
            }
        }
    }

    public boolean ticketAuthAvailable(Context context) {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return false;
        }
        return TicketAuthManager.getInstance().ticketAuthAvailable(context);
    }

    public void enableByPassMode() {
        this.mSdkMode = IConstants.SdkMode.BYPASS;
    }

    private void initCommonParams() {
        this.mSFAuth.setLoginParam(IVpnDelegate.AUTH_CONNECT_TIME_OUT, String.valueOf(this.mAuthConnectTimeOut));
        this.mSFAuth.setLoginParam(IVpnDelegate.AUTO_LOGIN_OFF_KEY, String.valueOf(this.mAutoLoginOff));
        this.mSFAuth.setLoginParam(IVpnDelegate.AUTH_DEVICE_LANGUAGE, this.mDefaultLanguage);
        SettingManager.getInstance().setGlobalValue(SettingManager.GLOBAL_AUTO_LOGIN_OFF, Boolean.valueOf(this.mAutoLoginOff));
        SettingManager.getInstance().setGlobalValue(SettingManager.GLOBAL_LANGUAGE, this.mDefaultLanguage);
    }

    private void initSdkMode(Context context) {
        if (VpnCommon.isEncapsulationApp(context)) {
            this.mSdkMode = IConstants.SdkMode.BYPASS;
        }
    }

    public void doRandCodeAuth(String str) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str)) {
            Log.error(TAG, "the graphic code is not set.");
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.SET_RND_CODE_STR, str);
            doVPNAuth(22);
        }
    }

    public void doPasswordAuth(String str, String str2) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str) || str2 == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mUserPassword = str2;
            this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_USERNAME, str);
            this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_PASSWORD, str2);
            doVPNAuth(1);
        }
    }

    public void doCertificateAuth(String str, String str2) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str) || str2 == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.CERT_P12_FILE_NAME, str);
            this.mSFAuth.setLoginParam(IVpnDelegate.CERT_PASSWORD, str2);
            doVPNAuth(0);
        }
    }

    public void doSMSAuth(String str) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str)) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.SMS_AUTH_CODE, str);
            doVPNAuth(2);
        }
    }

    public void doRadiusAuth(String str) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str)) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.CHALLENGE_AUTH_REPLY, str);
            doVPNAuth(6);
        }
    }

    public void doTokenAuth(String str) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (TextUtils.isEmpty(str)) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.TOKEN_AUTH_CODE, str);
            doVPNAuth(7);
        }
    }

    public void doRenewPasswordAuth(String str) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        String str2 = this.mUserPassword;
        if (str2 == null || str == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (TextUtils.equals(str2, str)) {
            sendFailedMessage(ErrorCode.SF_ERROR_NEW_PASSWORD_SAME_AS_OLD, SFError.getErrorDesc(ErrorCode.SF_ERROR_NEW_PASSWORD_SAME_AS_OLD));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_PASSWORD_NEW, str);
            doVPNAuth(18);
        }
    }

    public void doRenewPasswordAuth(String str, String str2) throws SFException {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || weakReference.get() == null) {
            Log.warn(TAG, "LoginResultListener is null");
            throw new SFException(EXCEPTION_LOGIN_RESULT_LISTENER);
        }
        this.mIsCallBacked = false;
        if (str == null || str2 == null) {
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_PARAM_EMPTY));
        } else if (TextUtils.equals(str, str2)) {
            sendFailedMessage(ErrorCode.SF_ERROR_NEW_PASSWORD_SAME_AS_OLD, SFError.getErrorDesc(ErrorCode.SF_ERROR_NEW_PASSWORD_SAME_AS_OLD));
        } else if (!NetworkHelper.isNetworkConnected(this.mApplication.getApplicationContext())) {
            Log.info(TAG, "Network is not enable.");
            sendFailedMessage(ErrorCode.SF_ERROR_NET_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_NET_INVALID));
        } else {
            this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_PASSWORD, str);
            this.mSFAuth.setLoginParam(IVpnDelegate.PASSWORD_AUTH_PASSWORD_NEW, str2);
            doVPNAuth(20);
        }
    }

    private boolean checkVPNInitParameters(Application application, Activity activity, IConstants.VPNMode vPNMode) {
        if (application == null || activity == null) {
            Log.error(TAG, "Application or context should not be null.");
            return false;
        } else if (vPNMode == null) {
            Log.error(TAG, "VPN type is invlid.");
            return false;
        } else {
            return true;
        }
    }

    private void vpnModeInit(Application application, Activity activity, IConstants.VPNMode vPNMode) throws SFException {
        if (vPNMode == IConstants.VPNMode.EASYAPP) {
            this.mVPNMode = 1;
        } else {
            this.mVPNMode = 2;
        }
        this.mSFAuth.init(application, activity, this, this.mVPNMode);
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void vpnAddressInit(String str) {
        try {
            if (!str.startsWith(IGeneral.PROTO_HTTPS_HEAD) && !str.startsWith(IGeneral.PROTO_HTTP_HEAD)) {
                str = String.format(Locale.getDefault(), "https://%s", str);
            }
            URL url = new URL(str);
            if (this.mSFAuth.vpnInit(VpnCommon.ipToLong(url.getHost()), url.getPort() < 0 ? 443 : url.getPort())) {
                return;
            }
            Log.error(TAG, "vpn init fail, error is " + this.mSFAuth.vpnGeterr());
            sendFailedMessage(ErrorCode.SF_ERROR_AUTH_INIT_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_INIT_ERROR));
        } catch (MalformedURLException e) {
            Log.error(TAG, "Malformed URL Error, ", e);
            sendFailedMessage(ErrorCode.SF_ERROR_ADDRESS_FORMAT, SFError.getErrorDesc(ErrorCode.SF_ERROR_ADDRESS_FORMAT));
        }
    }

    public void vpnLogout() {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        clearTicketInfo();
        if (this.mApplication != null) {
            NetworkMonitor.getInstance().stopMonitor(this.mApplication);
        }
        LineSelectionModel.getInstance().doCancelLineTask(LineSelectionTask.TaskType.CONNECT);
        this.mSFAuth.vpnLogout();
    }

    @Override // com.sangfor.ssl.IVpnDelegate
    public void vpnCallback(int i, int i2) {
        int i3;
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        ErrorCode errorCode = (ErrorCode) EnumHelperUtil.getByIntegerCode(ErrorCode.class, "value", this.mSFAuth.vpnGetErrorCode());
        String vpnGeterr = this.mSFAuth.vpnGeterr() == null ? "" : this.mSFAuth.vpnGeterr();
        if (i == -5) {
            Log.info(TAG, "L3VPN relogin.");
        } else if (i == -3) {
            Log.info(TAG, "L3VPN failed to start, error:" + vpnGeterr);
            sendFailedMessage(ErrorCode.SF_ERROR_START_L3VPN_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_START_L3VPN_FAILED));
        } else if (i == -2) {
            Log.info(TAG, "init vpn fail, error info:" + vpnGeterr + Log.getTimestamp());
            sendFailedMessage(errorCode, vpnGeterr);
        } else if (i == -1) {
            Log.info(TAG, "vpn auth fail, error info:" + vpnGeterr);
            if (i2 == 2048 && vpnGeterr.contains(Values.strings.AUTH_TICKET_FAILED)) {
                clearTicketInfo();
            }
            if (i2 == 18 && vpnGeterr.contains(Values.strings.FORCE_CHANGE_PASSWORD_ERROR)) {
                vpnGeterr = Values.strings.FORCE_CHANGE_PASSWORD_ERROR + "\n" + this.mSFAuth.getPasswordPolicy();
            }
            sendFailedMessage(errorCode, vpnGeterr);
        } else if (i == 1) {
            Log.info(TAG, "vpn init success, current vpn status is " + this.mSFAuth.vpnQueryStatus());
            Log.info(TAG, "VPN初始化成功,开始认证流程..." + Log.getTimestamp());
            AuthNativesManager.getInstance().reInitCertPublicKeyFile();
            doVPNAuth(this.mLoginType);
        } else if (i != 2) {
            if (i == 3) {
                notifyStatusChanged(ReasonCode.STATUS_USER_LOGOUT.value(), "");
            } else if (i == 4) {
                Log.info(TAG, "vpn auth cancel.");
                sendFailedMessage(ErrorCode.SF_ERROR_OTHER_ERROR, SFError.getErrorDesc(ErrorCode.SF_ERROR_OTHER_ERROR));
            } else if (i != 5) {
            } else {
                Log.info(TAG, "L3VPN startup successful.");
            }
        } else if (17 == i2) {
            Log.info(TAG, "welcome to sangfor sslvpn!");
            if (1 == this.mVPNMode) {
                notifyStatusChanged(ReasonCode.STATUS_LOGIN_SUCCESS.value(), "");
                sendSuccessMessage();
            } else {
                Log.info(TAG, "VPN认证成功,开始启动L3VPN服务..." + Log.getTimestamp());
            }
            saveTicketInfo(true);
        } else if (100 == i2) {
            Log.info(TAG, "L3VPN tunnel OK!" + Log.getTimestamp());
            waitForDetectTunnelSuccess();
        } else if (22 == i2 && 1 != (i3 = this.mLoginType)) {
            doVPNAuth(i3);
        } else {
            Log.info(TAG, "auth success, and need next auth, next auth type is " + i2);
            sendProcessMessage(i2, getAuthMessage(i2));
        }
    }

    private void waitForDetectTunnelSuccess() {
        final String vpnDomain = SystemConfiguration.getInstance().getVpnDomain();
        final Handler handler = new Handler(Looper.getMainLooper());
        handler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.1
            @Override // java.lang.Runnable
            public void run() {
                new DetectTunnelOnlineTask(handler, this).executeOnExecutor(SangforAuthManager.VPN_AUTH_TASK_EXECUTOR, vpnDomain);
            }
        });
    }

    /* loaded from: classes2.dex */
    class DetectTunnelOnlineTask extends AsyncTask<String, Void, Boolean> {
        private Handler mMainHandler;
        private Runnable mRunnable;

        public DetectTunnelOnlineTask(Handler handler, Runnable runnable) {
            this.mMainHandler = null;
            this.mRunnable = null;
            this.mMainHandler = handler;
            this.mRunnable = runnable;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public Boolean doInBackground(String... strArr) {
            boolean z = true;
            if (strArr != null) {
                try {
                } catch (Exception e) {
                    Log.error(SangforAuthManager.TAG, "detect online with an exception", e);
                }
                if (strArr.length == 1) {
                    String str = strArr[0];
                    Log.info(SangforAuthManager.TAG, "vpnHost: " + str);
                    z = DTSangforTunnelSocket.detectTunnel(str);
                    return Boolean.valueOf(z);
                }
            }
            z = false;
            return Boolean.valueOf(z);
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public void onPostExecute(Boolean bool) {
            SangforAuthManager.access$004();
            Log.info(SangforAuthManager.TAG, "detectTunnel count=" + SangforAuthManager.mCurrentDetectTunnelCount + " online=" + bool);
            if (SangforAuthManager.mCurrentDetectTunnelCount >= 100) {
                bool = true;
            }
            if (bool.booleanValue()) {
                Log.info(SangforAuthManager.TAG, "detect tunnel online ok!" + Log.getTimestamp());
                SangforAuthManager.this.notifyStatusChanged(ReasonCode.STATUS_LOGIN_SUCCESS.value(), "");
                SangforAuthManager.this.sendSuccessMessage();
                int unused = SangforAuthManager.mCurrentDetectTunnelCount = 0;
                return;
            }
            this.mMainHandler.postDelayed(this.mRunnable, 100L);
        }
    }

    private void saveTicketInfo(boolean z) {
        Log.info(TAG, "save ticket info");
        TicketAuthManager.getInstance().saveTicketInfo(z);
    }

    private void clearTicketInfo() {
        Log.info(TAG, "clear ticket info");
        TicketAuthManager.getInstance().clearTicketInfo();
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void notifyStatusChanged(int i, String str) {
        final IConstants.VPNStatus queryStatus = queryStatus();
        if (queryStatus != sVpnStatus) {
            Log.debug(TAG, "sVpnStatus:" + sVpnStatus + " vpnStatus:" + queryStatus + " current Threadid: " + Process.myTid());
            final StatusChangedReason statusChangedReason = new StatusChangedReason();
            statusChangedReason.reasonCode = ReasonCode.valueOf(i);
            statusChangedReason.reasonDes = str;
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.2
                @Override // java.lang.Runnable
                public void run() {
                    StatusChangeManager.getInstance().notifyStatusListener(queryStatus, statusChangedReason);
                }
            });
            sVpnStatus = queryStatus;
            if (AnonymousClass8.$SwitchMap$com$sangfor$ssl$common$ReasonCode[statusChangedReason.reasonCode.ordinal()] != 1) {
                return;
            }
            vpnLogout();
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    /* renamed from: com.sangfor.ssl.SangforAuthManager$8  reason: invalid class name */
    /* loaded from: classes2.dex */
    public static /* synthetic */ class AnonymousClass8 {
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$IConstants$DeviceLanguage;
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$IConstants$LogLevel;
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$common$ReasonCode;
        static final /* synthetic */ int[] $SwitchMap$com$sangfor$ssl$l3vpn$service$VpnStatus;

        static {
            int[] iArr = new int[VpnStatus.values().length];
            $SwitchMap$com$sangfor$ssl$l3vpn$service$VpnStatus = iArr;
            try {
                iArr[VpnStatus.VPN_STATUS_CONNECTED.ordinal()] = 1;
            } catch (NoSuchFieldError unused) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$l3vpn$service$VpnStatus[VpnStatus.VPN_STATUS_RECONNECTING.ordinal()] = 2;
            } catch (NoSuchFieldError unused2) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$l3vpn$service$VpnStatus[VpnStatus.VPN_STATUS_DISCONNECTED.ordinal()] = 3;
            } catch (NoSuchFieldError unused3) {
            }
            int[] iArr2 = new int[ReasonCode.values().length];
            $SwitchMap$com$sangfor$ssl$common$ReasonCode = iArr2;
            try {
                iArr2[ReasonCode.STATUS_VPNINFO_ERROR.ordinal()] = 1;
            } catch (NoSuchFieldError unused4) {
            }
            int[] iArr3 = new int[IConstants.LogLevel.values().length];
            $SwitchMap$com$sangfor$ssl$IConstants$LogLevel = iArr3;
            try {
                iArr3[IConstants.LogLevel.WARN.ordinal()] = 1;
            } catch (NoSuchFieldError unused5) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$IConstants$LogLevel[IConstants.LogLevel.ERROR.ordinal()] = 2;
            } catch (NoSuchFieldError unused6) {
            }
            try {
                $SwitchMap$com$sangfor$ssl$IConstants$LogLevel[IConstants.LogLevel.FATAL.ordinal()] = 3;
            } catch (NoSuchFieldError unused7) {
            }
            int[] iArr4 = new int[IConstants.DeviceLanguage.values().length];
            $SwitchMap$com$sangfor$ssl$IConstants$DeviceLanguage = iArr4;
            try {
                iArr4[IConstants.DeviceLanguage.ENGLISH.ordinal()] = 1;
            } catch (NoSuchFieldError unused8) {
            }
        }
    }

    private void notifyTunnelStatusChanged(VpnStatus vpnStatus) {
        final IConstants.VPNStatus vPNStatus;
        int i = AnonymousClass8.$SwitchMap$com$sangfor$ssl$l3vpn$service$VpnStatus[vpnStatus.ordinal()];
        if (i == 1) {
            vPNStatus = IConstants.VPNStatus.VPNONLINE;
        } else if (i == 2) {
            vPNStatus = IConstants.VPNStatus.VPNRECONNECTED;
        } else if (i != 3) {
            return;
        } else {
            vPNStatus = IConstants.VPNStatus.VPNOFFLINE;
        }
        IConstants.VPNStatus vPNStatus2 = sVpnStatus;
        if (vPNStatus2 != vPNStatus) {
            if (vPNStatus2 == IConstants.VPNStatus.VPNOFFLINE && vPNStatus == IConstants.VPNStatus.VPNRECONNECTED) {
                return;
            }
            Log.debug(TAG, "sVpnStatus: " + sVpnStatus + " vpnStatus: " + vPNStatus + " current Threadid: " + Process.myTid());
            sVpnStatus = vPNStatus;
            final StatusChangedReason statusChangedReason = new StatusChangedReason();
            statusChangedReason.reasonCode = ReasonCode.STATUS_TUNNEL_RECONNECTED;
            statusChangedReason.reasonDes = "";
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.3
                @Override // java.lang.Runnable
                public void run() {
                    StatusChangeManager.getInstance().notifyStatusListener(vPNStatus, statusChangedReason);
                }
            });
        }
    }

    private BaseMessage getAuthMessage(int i) {
        if (i == 2) {
            SmsMessage smsMessage = new SmsMessage();
            smsMessage.phoneNum = this.mSFAuth.getSmsPhoneNum();
            smsMessage.countDown = Integer.parseInt(this.mSFAuth.getSmsCountDown());
            return smsMessage;
        } else if (i == 6) {
            ChallengeMessage challengeMessage = new ChallengeMessage();
            challengeMessage.challengeMsg = this.mSFAuth.getChallengeMessage();
            return challengeMessage;
        } else if (i == 18 || i == 20) {
            ChangePswMessage changePswMessage = new ChangePswMessage();
            changePswMessage.policyMsg = this.mSFAuth.getPasswordPolicy();
            return changePswMessage;
        } else {
            return null;
        }
    }

    @Override // com.sangfor.ssl.IVpnDelegate
    public void vpnRndCodeCallback(byte[] bArr) {
        RandCodeListener randCodeListener;
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        WeakReference<RandCodeListener> weakReference = this.mRandCodeListener;
        if (weakReference == null || (randCodeListener = weakReference.get()) == null) {
            Log.warn(TAG, "RandCodeListener is null");
        } else if (bArr != null) {
            randCodeListener.onShowRandCode(Drawable.createFromStream(new ByteArrayInputStream(bArr), "rand_code"));
        } else {
            Log.error(TAG, "Randcode data is empty");
        }
    }

    public void statusChangedCallback(int i) {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        notifyStatusChanged(i, "");
    }

    private void doVPNAuth(int i) {
        if (this.mSdkMode == IConstants.SdkMode.BYPASS) {
            return;
        }
        Log.info(TAG, "start certification:" + i);
        if (i == 0 || i == 1 || i == 2 || i == 3 || i == 6 || i == 7 || i == 11 || i == 16 || i == 18 || i == 20 || i == 22 || i == 2048) {
            if (i == 22) {
                i = 1;
            }
            if (this.mSFAuth.vpnLogin(i)) {
                return;
            }
            Log.warn(TAG, "authentication failed.");
            sendFailedMessage((ErrorCode) EnumHelperUtil.getByIntegerCode(ErrorCode.class, "value", this.mSFAuth.vpnGetErrorCode()), this.mSFAuth.vpnGeterr() == null ? "" : this.mSFAuth.vpnGeterr());
            return;
        }
        Log.error(TAG, "incorrect authentication type.");
        sendFailedMessage(ErrorCode.SF_ERROR_AUTH_TYPE_UNSPPORT, SFError.getErrorDesc(ErrorCode.SF_ERROR_AUTH_TYPE_UNSPPORT));
    }

    private void startVpnLine(String str) {
        Log.info(TAG, "开始选路..." + Log.getTimestamp());
        LineSelectionModel.getInstance().init(this.mApplication.getApplicationContext());
        LineSelectionModel.getInstance().postLineSelectTask(str, LineSelectionTask.TaskType.CONNECT, this, null);
    }

    @Override // com.sangfor.ssl.service.line.SelectLineCallback
    public void onSelectLineSuccess(final LineResult lineResult, boolean z) {
        if (lineResult == null || TextUtils.isEmpty(lineResult.selectUrl)) {
            Log.warn(TAG, "select line success,but the address is empty");
            sendFailedMessage(ErrorCode.SF_ERROR_SELECT_LINE_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_SELECT_LINE_FAILED));
            return;
        }
        Log.info(TAG, "Select Line Success, url:" + lineResult.selectUrl);
        Log.info(TAG, "选路成功,开始初始化VPN...!" + Log.getTimestamp());
        if (LineSelectionModel.getInstance().saveSelectInfo(this.mAddress, lineResult)) {
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.4
                @Override // java.lang.Runnable
                public void run() {
                    new WaitForLogoutEndTask(lineResult.vpnAddress).executeOnExecutor(SangforAuthManager.VPN_AUTH_TASK_EXECUTOR, new Void[0]);
                }
            });
            return;
        }
        Log.info(TAG, "Save select line info failed.");
        sendFailedMessage(ErrorCode.SF_ERROR_SELECT_LINE_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_SELECT_LINE_FAILED));
    }

    @Override // com.sangfor.ssl.service.line.SelectLineCallback
    public void onSelectLineFailed(int i) {
        Log.error(TAG, "Select Line Failed:" + i);
        Log.info(TAG, "选路失败!" + Log.getTimestamp());
        switch (i) {
            case -6:
                sendFailedMessage(ErrorCode.SF_ERROR_DOMAN_RESOLVE_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_DOMAN_RESOLVE_FAILED));
                return;
            case -5:
                sendFailedMessage(ErrorCode.SF_ERROR_OUT_OF_MEMORY, SFError.getErrorDesc(ErrorCode.SF_ERROR_OUT_OF_MEMORY));
                return;
            case -4:
                sendFailedMessage(ErrorCode.SF_ERROR_ADDRESS_FORMAT, SFError.getErrorDesc(ErrorCode.SF_ERROR_ADDRESS_FORMAT));
                return;
            case -3:
                sendFailedMessage(ErrorCode.SF_ERROR_SELECT_LINE_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_SELECT_LINE_FAILED));
                return;
            case -2:
                sendFailedMessage(ErrorCode.SF_ERROR_CONNECT_VPN_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_CONNECT_VPN_FAILED));
                return;
            case -1:
                sendFailedMessage(ErrorCode.SF_ERROR_URL_INVALID, SFError.getErrorDesc(ErrorCode.SF_ERROR_URL_INVALID));
                return;
            default:
                sendFailedMessage(ErrorCode.SF_ERROR_SELECT_LINE_FAILED, SFError.getErrorDesc(ErrorCode.SF_ERROR_SELECT_LINE_FAILED));
                return;
        }
    }

    /* loaded from: classes2.dex */
    private class WaitForLogoutEndTask extends AsyncTask<Void, Void, Void> {
        String vpnAddress;

        public WaitForLogoutEndTask(String str) {
            this.vpnAddress = "";
            this.vpnAddress = str;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public Void doInBackground(Void... voidArr) {
            Log.info(SangforAuthManager.TAG, "WaitForLogoutEndTask begin" + Log.getTimestamp());
            return null;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // android.os.AsyncTask
        public void onPostExecute(Void r2) {
            Log.info(SangforAuthManager.TAG, "WaitForLogoutEndTask end" + Log.getTimestamp());
            SangforAuthManager.this.vpnAddressInit(this.vpnAddress);
        }
    }

    String dealVPNAddress(URL url) {
        this.mHostStr = url.getHost();
        String url2 = url.toString();
        this.mAddress = url2;
        return url2;
    }

    private void sendFailedMessage(final ErrorCode errorCode, final String str) {
        final LoginResultListener loginResultListener;
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || (loginResultListener = weakReference.get()) == null) {
            Log.warn(TAG, "LoginResultListener is null");
        } else {
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.5
                @Override // java.lang.Runnable
                public void run() {
                    if (SangforAuthManager.this.mIsCallBacked) {
                        return;
                    }
                    SangforAuthManager.this.mIsCallBacked = true;
                    loginResultListener.onLoginFailed(errorCode, str);
                }
            });
        }
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void sendSuccessMessage() {
        final LoginResultListener loginResultListener;
        Log.info(TAG, "VPN启动成功,欢迎使用!" + Log.getTimestamp());
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || (loginResultListener = weakReference.get()) == null) {
            Log.warn(TAG, "LoginResultListener is null");
        } else {
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.6
                @Override // java.lang.Runnable
                public void run() {
                    if (SangforAuthManager.this.mIsCallBacked) {
                        return;
                    }
                    SangforAuthManager.this.mIsCallBacked = true;
                    loginResultListener.onLoginSuccess();
                }
            });
        }
    }

    private void sendProcessMessage(final int i, final BaseMessage baseMessage) {
        final LoginResultListener loginResultListener;
        WeakReference<LoginResultListener> weakReference = this.mLoginResultListener;
        if (weakReference == null || (loginResultListener = weakReference.get()) == null) {
            Log.warn(TAG, "LoginResultListener is null");
        } else {
            this.mainHandler.post(new Runnable() { // from class: com.sangfor.ssl.SangforAuthManager.7
                @Override // java.lang.Runnable
                public void run() {
                    if (SangforAuthManager.this.mIsCallBacked) {
                        return;
                    }
                    SangforAuthManager.this.mIsCallBacked = true;
                    loginResultListener.onLoginProcess(i, baseMessage);
                }
            });
        }
    }

    @Override // com.sangfor.ssl.IVpnDelegate
    public void vpnStatusCallback(VpnStatus vpnStatus) {
        notifyTunnelStatusChanged(vpnStatus);
    }

    public String packLogs(String str) throws Exception {
        return Log.packLogs(str);
    }

    public String uploadLogsToSangfor(String str) throws Exception {
        return Log.uploadLogsToSangfor(str);
    }

    public Boolean detectTunnel(String str) throws MalformedURLException {
        if (!str.startsWith(IGeneral.PROTO_HTTPS_HEAD) && !str.startsWith(IGeneral.PROTO_HTTP_HEAD)) {
            str = String.format(Locale.getDefault(), "https://%s", str);
        }
        return Boolean.valueOf(DTSangforTunnelSocket.detectTunnel(new URL(str).getHost()));
    }

    public void startCapture() {
        String absolutePath = SangforNbAuth.getInstance().getContext().getExternalFilesDir(null).getAbsolutePath();
        File file = new File(absolutePath + "/sangfor");
        if (!file.isDirectory() && !file.mkdirs()) {
            Log.warn(TAG, "start capture failed! create dir failed! " + file);
            return;
        }
        File file2 = new File(file, "test.pcap");
        Log.info(TAG, "startCapturePacket....s capFile:" + file2);
        boolean startCapturePacket = VpnServiceManager.getInstance().startCapturePacket(file2.getAbsolutePath(), 262144, 1000000000L, 104857600L, false);
        Log.info(TAG, "startCapturePacket....e running:" + startCapturePacket);
    }

    public void stopCapture() {
        Log.info(TAG, "stopCapturePacket....s");
        VpnServiceManager.getInstance().stopCapturePacket();
        boolean isCapturing = VpnServiceManager.getInstance().isCapturing();
        Log.info(TAG, "stopCapturePacket....e isCapturing:" + isCapturing);
    }
}
